<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
>
<head>
    <!-- 设置页面的 基本路径，页面所有资源引入和页面的跳转全部基于 base路径 -->
    <base th:href="${#request.getContextPath()}+'/'">
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <link rel="Bookmark" href="/favicon.ico">
    <link rel="Shortcut Icon" href="/favicon.ico"/>

    <script type="text/javascript" th:src="@{/lib/html5shiv.js}"></script>
    <script type="text/javascript" th:src="@{/lib/respond.min.js}"></script>
    <link rel="stylesheet" type="text/css" th:href="@{/h-ui/css/H-ui.min.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/h-ui.admin/css/H-ui.admin.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/lib/Hui-iconfont/1.0.8/iconfont.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/h-ui.admin/skin/default/skin.css}" id="skin"/>
    <link rel="stylesheet" type="text/css" th:href="@{/h-ui.admin/css/style.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/lib/bootstrap/css/bootstrap.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/lib/bootstrap-table/bootstrap-table.css}"/>


    <script type="text/javascript" th:src="@{/lib/DD_belatedPNG_0.0.8a-min.js}"></script>
    <script>DD_belatedPNG.fix('*');</script>

    <title>采购入库管理</title>
</head>
<body>
<nav class="breadcrumb" style="margin-bottom: 5px"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span
        class="c-gray en">&gt;</span> 采购入库管理 <span class="c-gray en">&gt;</span>采购入库列表
</nav>
<div class="page-container" style="padding: 0 20px">
    <span id="toolbar" class="l">
        <shiro:hasPermission name="procurement:insert">
        <a href="javascript:;" onclick="medicine_add()"
           class="btn btn-primary radius"><i class="Hui-iconfont">&#xe600;</i> 添加药品</a>
        </shiro:hasPermission>

        <shiro:hasPermission name="procurement:update">
        <a href="javascript:;" onclick="medicines_info_set()"
           class="btn btn-primary radius"><i class="Hui-iconfont">&#xe600;</i> 批量设置药品信息</a>
        </shiro:hasPermission>

        <shiro:hasPermission name="procurement:examine">
        <a href="javascript:;" onclick="examine()"
           class="btn btn-primary radius"><i class="Hui-iconfont">&#xe600;</i> 提交审核</a>
        </shiro:hasPermission>
        <shiro:hasPermission name="procurement:warehousing">
         <a href="javascript:;" onclick="warehousing()"
            class="btn btn-primary radius"><i class="Hui-iconfont">&#xe600;</i> 直接入库</a>
        </shiro:hasPermission>

        <br><br>
        <span>单据号:  </span><span id="oddNumbers" th:text="${oddNumbers}"></span>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <span>制单日期: </span><input type="date" id="billingDate" name="billingDate" placeholder="">
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <span class="select-box" style="width:150px;">
                    <select class="select" id="supplierId" name="supplierId" size="1">
                        <option value="0">请选择供应商</option>
                        <!--当前修改的管理员的角色id和循环的角色的id只要相等，就选中-->
                        <option th:each="supplier:${suppliers}"
                                th:value="${supplier.supplierId}"
                                th:text="${supplier.supplier}"></option>
                    </select>
        </span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <span>制单人: </span><shiro:principal property="realname"/>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <br>
        <span>批发总金额: </span> <span id="totalWholesaleAmount"></span>
        &nbsp;&nbsp;
        <span>处方总金额:</span> <span id="totalAmountPrescription"></span>

    </span>

    <div id="operateDiv" style="display: none;">
        <shiro:hasPermission name="procurement:delete">
            <a title="删除" href="javascript:;" id="procurement_delete" value="" onclick="procurement_del()" class="ml-5"
               style="text-decoration:none"><i class="Hui-iconfont"></i></a>
        </shiro:hasPermission>
    </div>


    <table id="dataTable" class="table table-border table-bordered table-bg">
    </table>
</div>
<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" th:src="@{/lib/jquery/1.11.3/jquery.min.js}"></script>
<script type="text/javascript" th:src="@{/lib/layer/2.4/layer.js}"></script>
<script type="text/javascript" th:src="@{/h-ui/js/H-ui.min.js}"></script>
<script type="text/javascript" th:src="@{/h-ui.admin/js/H-ui.admin.js}"></script> <!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" th:src="@{/lib/My97DatePicker/4.8/WdatePicker.js}"></script>
<script type="text/javascript" th:src="@{/lib/datatables/1.10.0/jquery.dataTables.min.js}"></script>
<script type="text/javascript" th:src="@{/lib/bootstrap-table/bootstrap-table.js}"></script>
<script type="text/javascript" th:src="@{/lib/bootstrap-table/bootstrap-table-zh-CN.min.js}"></script>


<script type="text/javascript">

    function cmputer() {
        $.get("procurement/computer", function (data) {

            $("#totalWholesaleAmount").text(data.totalWholesaleAmount);
            $("#totalAmountPrescription").text(data.totalAmountPrescription);

        })
    }

    $(function () {
        cmputer();
        $('#dataTable').bootstrapTable({
            url: 'procurement/list',//ajax请求的url地址
            pagination: true,
            toolbar: "#toolbar",//顶部显示的工具条（添加和批量删除的）
            contentType: 'application/x-www-form-urlencoded',//条件搜索的时候ajax请求给后台数据的数据类型（条件搜索post提交必须设置）
            pageNumber: 1,//默认的页面 第一页
            pageSize: 10,//默认的每页条数
            pageList: [10, 15, 50, 100],//每页能显示的条数
            sidePagination: "server",//是否是服务器分页，每次请求都是对应的10条数据，下一页发送ajax请求
            paginationHAlign: 'right', //底部分页条
            showRefresh: true, //是否显示刷新按钮
            columns: [ //表格显示数据对应的表头设置，
                {checkbox: true},//是否显示前台的复选框（多选）
                /*
                    每列数据的表头的设置
                    filed:返回json数据对应数据的key
                    title:表头要显示的名
                */
                {field: 'procurementId', title: '编号'},
                {field: 'medicineName', title: '药品名称'},
                {field: 'specs', title: '规格'},
                {field: 'unit', title: '单位'},
                {field: 'procurementManufacturer', title: '生产厂家'},
                {field: 'procurementNumber', title: '数量', align: "center", formatter: numberFormatter},
                {
                    field: 'procurementWholesalePrice', title: '批发价', align: "center",
                    formatter: wholesalePricenumberFormatter
                },
                {
                    field: 'procurementPrescriptionPrice', title: '处方价', align: "center",
                    formatter: prescriptionPricenumberFormatter
                },
                {
                    field: 'procurementWholesaleAmount', title: '批发额', align: "center",
                    formatter: procurementWholesaleAmountFormatter
                },
                {
                    field: 'procurementPrescriptionAmount', title: '处方额', align: "center",
                    formatter: procurementPrescriptionAmountFormatter
                },
                //操作列的设置（删除，修改）
                /*
                formatter: 格式化这一行，回调一个函数
                */
                {
                    field: 'procurementId',
                    title: '操作',
                    align: 'center',
                    formatter: getOperateBtns

                }],
            queryParams: function (params) {
                //此方法在用户分页或者搜索的时候回自动发送ajax请求调用，并把对应的参数传递给后台
                return {
                    pageNum: params.offset / params.limit + 1, //页码
                    pageSize: params.limit, //页面大小
                    keyword: params.search
                };
            }
        });

    });

    function getOperateBtns(value, row, index) {
        var html = "";
        $("#operateDiv").find("a").each(function () {
            var myA = $(this).clone()
            myA.attr("value", row.procurementId)
            html += myA.prop("outerHTML") + "&nbsp;&nbsp;&nbsp;&nbsp;";

        });

        return html;
    }

    function numberFormatter(value, row, index) {
        var html = '<div class="fublBox" style="width: 100px;margin: 0 auto"><input value=' + row.procurementNumber + ' disabled="true" id="procurementNumber" name="procurementNumber" type="number" class="form-control form-itmeB formNumber"/></div>';
        return html;
    }

    function wholesalePricenumberFormatter(value, row, index) {
        var html = '<div class="fublBox" style="width: 100px;margin: 0 auto"><input value=' + row.procurementWholesalePrice + ' disabled="true" onchange="compute_procurementProcurementWholesaleAmount()" onkeyup="compute_procurementProcurementWholesaleAmount()" id="procurementWholesalePrice" name="procurementWholesalePrice" type="number" class="form-control form-itmeB formNumber"/></div>';
        return html;
    }

    function prescriptionPricenumberFormatter(value, row, index) {
        var html = '<div class="fublBox" style="width: 100px;margin: 0 auto"><input value=' + row.procurementPrescriptionPrice + ' disabled="true" onchange="compute_procurementProcurementPrescriptionAmount()" onkeyup="compute_procurementProcurementPrescriptionAmount()" id="procurementPrescriptionPrice" name="procurementPrescriptionPrice" type="number" class="form-control form-itmeB formNumber"/></div>';
        return html;
    }

    function procurementWholesaleAmountFormatter(value, row, index) {
        var html = '<div class="fublBox" style="width: 100px;margin: 0 auto"><input value=' + row.procurementWholesaleAmount + ' disabled="true" id="procurementProcurementWholesaleAmount" name="procurementProcurementWholesaleAmount" type="number" class="form-control form-itmeB formNumber"/></div>';
        return html;
    }

    function procurementPrescriptionAmountFormatter(value, row, index) {
        var html = '<div class="fublBox" style="width: 100px;margin: 0 auto"><input value=' + row.procurementPrescriptionAmount + ' disabled="true" id="procurementProcurementPrescriptionAmount" name="procurementProcurementPrescriptionAmount" type="number" class="form-control form-itmeB formNumber"/></div>';
        return html;
    }

</script>


<script type="text/javascript">

    $(function () {
        var a = $("#procurementNumber").val();
        var b = $("#procurementWholesalePrice").val();
        $("#procurementProcurementWholesaleAmount").val(a * b);
        var c = $("#procurementNumber").val();
        var d = $("#procurementPrescriptionPrice").val();
        $("#procurementProcurementPrescriptionAmount").val(c * d);
    })

    /*药品-增加*/
    function medicine_add() {
        layer.open({
            type: 2,
            title: "添加药品",
            shade: [0],
            maxmin: true,
            area: ["800px", "600px"],
            content: "procurement/procurementToAdd", //iframe的url，no代表不显示滚动条
        });
    }

    function warehousing() {

        layer.confirm('确认不用审核直接入库吗？', function () {
            var oddNumbers = $("#oddNumbers").text();
            var billingDate = $("#billingDate").val();
            var supplierId = $("#supplierId").val();
            var totalAmount = parseInt($("#totalWholesaleAmount").text()) + parseInt($("#totalAmountPrescription").text());

            if ($("#procurementNumber").val().length==0){
                layer.msg("药品数据不能为空", {icon:2, time: 1000},function () {

                })
                return false;
            }

            if (supplierId > 0 && billingDate.length > 0) {
                $.get("procurement/warehousing", {
                    "oddNumbers": oddNumbers,
                    "billingDate": billingDate,
                    supplierId: supplierId,
                    "totalAmount": totalAmount
                }, function (data) {
                    layer.msg(data.msg, {icon: data.code, time: 1000}, function () {
                        //操作成功刷新数据
                        if (data.code == 1) {
                            refresh();
                        }
                    });
                })
            } else {
                layer.msg("供应商或日期不能为空", {icon: 1, time: 500}, function () {
                    //操作成功刷新数据
                    refresh();
                });
            }

        });


    }

    function examine() {
        var oddNumbers = $("#oddNumbers").text();
        var billingDate = $("#billingDate").val();
        var supplierId = $("#supplierId").val();
        var totalAmount = parseInt($("#totalWholesaleAmount").text()) + parseInt($("#totalAmountPrescription").text());

        if ($("#procurementNumber").val().length==0){
            layer.msg("药品数据不能为空", {icon:2, time: 1000},function () {

            })
            return false;
        }

        if (supplierId > 0 && billingDate.length > 0) {
            $.get("procurement/examine", {
                "oddNumbers": oddNumbers,
                "billingDate": billingDate,
                supplierId: supplierId,
                "totalAmount": totalAmount
            }, function (data) {
                layer.msg(data.msg, {icon: data.code, time: 1000}, function () {
                    //操作成功刷新数据
                    if (data.code == 1) {
                        refresh();
                    }
                });
            })
        } else {
            layer.msg("供应商或日期不能为空", {icon: 1, time: 500}, function () {
                //操作成功刷新数据
                refresh();
            });
        }

    }

    /*管理员-删除*/
    function procurement_del(obj) {
        var procurementId = obj.getAttribute("value")
        layer.confirm('确认要删除吗？', function () {

            $.get("procurement/delete?procurementId=" + procurementId, function (data) {

                //操作结果提示
                layer.msg(data.msg, {icon: data.code, time: 500}, function () {
                    //操作成功刷新数据
                    if (data.code == 1) {
                        refresh();

                    }
                });

            })

        });
    }

    function medicines_info_set() {
        var rows = $("#dataTable").bootstrapTable('getSelections')
        //提示
        if (rows.length == 0) {

            layer.msg('至少选中一条', {icon: 0, time: 1000})
            return false
        }

        var procurementIds = []

        rows.forEach(function (row) {
            procurementIds.push(row.procurementId)
        })
        console.log(procurementIds);
        layer.open({
            type: 2,
            title: "批量设置",
            shade: [0],
            maxmin: true,
            area: ["600px", "400px"],
            content: "procurement/toMedicinesSet?procurementIds=" + procurementIds, //iframe的url，no代表不显示滚动条
        });
    }


    function compute_procurementProcurementWholesaleAmount() {
        var a = $("#procurementNumber").val();
        var b = $("#procurementWholesalePrice").val();
        $("#procurementProcurementWholesaleAmount").val(a * b);
        cmputer();
    }

    function compute_procurementProcurementPrescriptionAmount() {
        var c = $("#procurementNumber").val();
        var d = $("#procurementPrescriptionPrice").val();
        $("#procurementProcurementPrescriptionAmount").val(c * d);
        cmputer();
    }

    function refresh() {
        $('#dataTable').bootstrapTable('refresh');
        cmputer();
    }


</script>
</body>
</html>
